package com.xnyzc.lhy.didi.controller.platform;

import com.alibaba.fastjson.JSON;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.didi.entity.param.xiecheng.request.*;
import com.xnyzc.lhy.didi.entity.param.xiecheng.request.chatered.*;
import com.xnyzc.lhy.didi.entity.param.xiecheng.response.XieChengResponse;
import com.xnyzc.lhy.didi.service.xiecheng.XcCharteredService;
import com.xnyzc.lhy.didi.util.XieChengUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.ParseException;

@Api(value = "携程包车订单API", tags = {"携程包车订单API"})
@Slf4j
@RestController
@RequestMapping("oa/xc/day/")
public class XieChengCharteredController {

    //供应商ID
    @Value("${global.xc.CharteredVenderId:1001108}")
    private String venderId;

    //携程密钥
    @Value("${global.xc.CharteredSecretKey:12345678}")
    private String secretKey;


    @Autowired
    private XcCharteredService requestService;

    @RequestMapping(value = "{operation}/{version}/{timeSpan}/{sign}", method = RequestMethod.POST)
    @ResponseBody
    public XieChengResponse queryProduct(@PathVariable("operation") String action, @PathVariable("version") String version,
                                         @PathVariable("timeSpan") String timestamp, @PathVariable("sign") String sign,
                                         HttpServletRequest request) throws ParseException {
        String body = "";
        BufferedReader br = null;
        try {
            br = request.getReader();
            String str = "";
            while ((str = br.readLine()) != null) {
                body += str;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String msign = XieChengUtil.getCharteredSign(venderId, version, timestamp, body.length(), secretKey);
        if (msign.equalsIgnoreCase(sign)) {
            String params = null;
            try {
                //log.info("Body-" + body);
                params = XieChengUtil.decrypt(body, secretKey);
                //log.info("params-" + params);
            } catch (Exception e) {
                throw new PangException(e.getMessage());
            }
            //接口的处理器
            return handleAction(action, params);
        } else {
            XieChengResponse response = XieChengResponse.error();
            response.setMessage("日租车总入口校验失败");
            return response;
        }
    }

    /**
     * 总接口的处理器
     *
     * @param action 接口类型
     * @param body   传入的参数
     * @return
     */
    private XieChengResponse handleAction(String action, String body) throws ParseException {
        if (CheckUtil.strIsEmpty(body)) {
            return XieChengResponse.error();
        }
        switch (action) {
            case "estimatePrice": {
                log.info("携程日租车查询预估价：{}", body);
                CharteredRequestDTO request = JSON.parseObject(body, CharteredRequestDTO.class);
                return requestService.queryCharteredProduct(request);
            }
            case "createOrder": {
                log.info("创建日租车携程订单参数：{}", body);
                CreateCharteredOrderDto request = JSON.parseObject(body, CreateCharteredOrderDto.class);
                return requestService.createCharteredOrder(request);
            }
            case "cancelOrder": {
                log.info("携程日租车取消订单参数：{}", body);
                CancelCharteredOrderDTO request = JSON.parseObject(body, CancelCharteredOrderDTO.class);
                return requestService.cancelCharteredOrder(request);
            }
            case "queryOrder": {
                log.info("携程日租车查询定单详情：{}", body);
                QueryCharteredOrderInfoDTO request = JSON.parseObject(body, QueryCharteredOrderInfoDTO.class);
                return requestService.queryCharteredOrder(request);
            }
            case "payAdditionalFee": {
                log.info("携程日租车已支付套餐外费用：{}", body);
                PayAdditionalFeeDTO request = JSON.parseObject(body, PayAdditionalFeeDTO.class);
                return requestService.payCharteredAdditionalFee(request);
            }
            case "updateOrder": {
                log.info("携程日租车修改订单：{}", body);
                UpdateCharteredOrderDTO request = JSON.parseObject(body, UpdateCharteredOrderDTO.class);
                return requestService.updateCharteredOrder(request);
            }
            default: {
                return XieChengResponse.error();
            }
        }
    }

    @RequestMapping(value = "/testPay", method = RequestMethod.POST)
    public XieChengResponse testPay(@RequestBody PayAdditionalFeeDTO request) throws ParseException {
        log.info("携程获取配置的订单价格参数：{}", request);
        return requestService.payCharteredAdditionalFee(request);
    }
    /**
     * 开发者测试使用
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/testCreate", method = RequestMethod.POST)
    public XieChengResponse testCreate(@RequestBody CreateCharteredOrderDto request) throws ParseException {
        log.info("携程获取配置的订单价格参数：{}", request);
        return requestService.createCharteredOrder(request);
    }

    @RequestMapping(value = "/testPrice", method = RequestMethod.POST)
    public XieChengResponse testPrice(@RequestBody CharteredRequestDTO request) throws ParseException {
        log.info("携程获取配置的订单价格参数：{}", request);
        return requestService.queryCharteredProduct(request);
    }
    @RequestMapping(value = "/testCancel", method = RequestMethod.POST)
    public XieChengResponse testCancel(@RequestBody CancelCharteredOrderDTO request) throws ParseException {
        log.info("携程获取配置的订单价格参数：{}", request);
        return requestService.cancelCharteredOrder(request);
    }
    @RequestMapping(value = "/testQuery", method = RequestMethod.POST)
    public XieChengResponse testQuery(@RequestBody QueryCharteredOrderInfoDTO request) throws ParseException {
        log.info("携程获取配置的订单价格参数：{}", request);
        return requestService.queryCharteredOrder(request);
    }
    @RequestMapping(value = "/testUpdate", method = RequestMethod.POST)
    public XieChengResponse testUpdate(@RequestBody UpdateCharteredOrderDTO request) throws ParseException {
        log.info("携程获取配置的订单价格参数：{}", request);
        return requestService.updateCharteredOrder(request);
    }

}
